<?php

namespace App\Http\Controllers\Admin;

use App\Models\Pages;

class PagesController extends BaseController
{

    /**
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     * @throws \Exception
     */
    public function index(){
        if ($this->isOnSubmit()){
            $delete = $this->request->post('delete');
            if ($delete) {
                foreach ($delete as $pageid){
                    Pages::where('pageid', $pageid)->delete();
                }
            }

            $pages = $this->request->input('pages');
            if ($pages) {
                foreach ($pages as $pageid=>$page){
                    Pages::where('pageid', $pageid)->update($page);
                }
            }
            return $this->messager()->message(trans('sysmessage.info save success'))->render();
        }else {
            $catid = $this->request->get('catid');

            $condition = $catid ? ['catid'=>$catid] : [];
            $items = Pages::onlyPage()->where($condition)->orderBy('displayorder')->paginate(10);
            $this->assign([
                'catid'=>$catid,
                'items'=>$items,
                'pagination'=>$items->appends($condition)->render(),
                'categories'=>Pages::scopes(['onlyCategory'])->orderBy('displayorder')->get()
            ]);

            return $this->view('admin.pages.pages');
        }
    }

    /**
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function newpage(){
        $pageid = $this->request->get('pageid');
        if ($this->isOnSubmit()){
            $newpage = $this->request->post('newpage');
            if ($pageid) {
                $newpage['updated_at'] = time();
                Pages::where('pageid', $pageid)->update($newpage);
            }else {
                $newpage['type'] = 'page';
                $newpage['created_at'] = time();
                Pages::insert($newpage);
            }
            return $this->messager()->message(trans('sysmessage.info save success'))->render();
        }else {

            $this->assign([
                'catid'=>$this->request->get('catid'),
                'pageid'=>$pageid,
                'page'=>[
                    'title'=>'',
                    'alias'=>'',
                    'template'=>'',
                    'summary'=>'',
                    'content'=>''
                ],
                'categories'=>Pages::onlyCategory()->get()
            ]);

            if ($pageid) {
                $page = Pages::find($pageid);
                if ($page) {
                    $this->assign([
                        'page'=>$page,
                        'catid'=>$page->catid,
                    ]);
                }
            }

            return $this->view('admin.pages.newpage');
        }
    }

    /**
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     * @throws \Exception
     */
    public function category(){
        if ($this->isOnSubmit()){
            $delete = $this->request->post('delete');
            if ($delete) {
                foreach ($delete as $pageid){
                    Pages::where('catid', $pageid)->delete();
                    Pages::where('pageid', $pageid)->delete();
                }
            }

            $categories = $this->request->post('categories');
            if ($categories) {
                foreach ($categories as $pageid=>$category){
                    if ($category['title']) {
                        $category['updated_at'] = time();
                        if ($pageid > 0) {
                            Pages::where('pageid', $pageid)->update($category);
                        }else {
                            $category['type'] = 'category';
                            $category['created_at'] = time();
                            Pages::insert($category);
                        }
                    }
                }
            }
            return $this->messager()->message(trans('sysmessage.info save success'))->render();
        }else {

            $this->assign([
                'categories'=>Pages::onlyCategory()->orderBy('displayorder')->get()
            ]);

            return $this->view('admin.pages.category');
        }
    }
}
